<?php

/**

 * 微信授权相关接口
 */

class Wchat

{
      private $app_id = 'wx6baf085937bbfcba';

      private $app_secret = 'c471711e03af927d856bf171e71b3d43';

     private $state='aaaa';

   /**

     * 获取微信授权链接

     * 

     * @param string $redirect_uri 跳转地址

     * @param mixed $state 参数

     */

    public function get_authorize_url($redirect_uri = '', $state = '')

    {

        $redirect_uri = urlencode($redirect_uri);

        return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";

    }

     /**

     * 获取微信openid

     */


    /**

     * 获取授权token网页授权

     * 

     * @param string $code 通过get_authorize_url获取到的code

     */

    public function get_access_token($code = '')

    {

      $appid=$this->app_id;

      $appsecret=$this->app_secret;

       

		$token_url ="https://api.weixin.qq.com/sns/jscode2session?appid=".$appid."&secret=".$appsecret."&js_code=".$code."&grant_type=authorization_code";

        $token_data = $this->http($token_url);
		return $token_data[1];
		// return;

        // var_dump( $token_data);
		// exit();

        // if($token_data[0] == 200)

        // {

            // $ar=json_decode($token_data[1], TRUE);

            // return $ar;

        // }

         

        // return $token_data[1];

    }

     

     

    public function http($url, $method='', $postfields = null, $headers = array(), $debug = false)

    {

        $ci = curl_init();

        /* Curl settings */

        curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

        curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);

        curl_setopt($ci, CURLOPT_TIMEOUT, 30);

        curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

  

        switch ($method) {

            case 'POST':

                curl_setopt($ci, CURLOPT_POST, true);

                if (!empty($postfields)) {

                    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);

                    $this->postdata = $postfields;

                }

                break;

        }

        curl_setopt($ci, CURLOPT_URL, $url);

        curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);

        curl_setopt($ci, CURLINFO_HEADER_OUT, true);

  

        $response = curl_exec($ci);

        $http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

  

        if ($debug) {

            echo "=====post data======\r\n";

            var_dump($postfields);

  

            echo '=====info=====' . "\r\n";

            print_r(curl_getinfo($ci));

  

            echo '=====$response=====' . "\r\n";

            print_r($response);

        }

        curl_close($ci);

        return array($http_code, $response);

    }

  

}

		$wchat=new wchat();

		 $code = $_GET['code'];
          $info=$wchat->get_access_token($code);
		  echo $info;
		 



?>
